<?php
// +----------------------------------------------------------------------
// | [ 简单 高效 卓越 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.lingyun.net All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Model;

use Common\Model\ModelModel;

class GoodsModel extends ModelModel
{
	/**
     * 数据库表名
     * @author jry <598821125@qq.com>
     */
    protected $tableName = 'admin_goods';
	
	/**
     * 自动验证规则
     * @author jry <598821125@qq.com>
     */
    //protected $_validate = array(
        //验证品牌名称
       // array('brand_name', 'require', '品牌名称不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
		//验证品牌名称
        //array('img_id', 'require', '请上传缩略图', self::EXISTS_VALIDATE, 'regex', self::MODEL_BOTH),
        //验证品牌名称
       // array('brand_name', '', '品牌名称已存在', self::MUST_VALIDATE, 'unique', self::MODEL_BOTH),

   // );


	/**
	 * 处理导入的数据
	 * @param  number $brand_id 品牌ID
	 * @param  array  $datas 数据数组
	 */
	public function set_sql_excel($brand_id,$datas = ""){
		
		// echo $type;
		// echo "<pre>";
		// print_r($datas);
		// echo "</pre>";
		// die("asd");
		
		/*商品数据*/
		$data = array();
		$data['brand_id'] = $brand_id;
		$data['goods_name'] = str_replace(' ','',$datas['goods_name']);
		$data['goods_type_memory'] = str_replace(' ','',$datas['goods_type_memory']);
		$data['goods_type_colour'] = str_replace(' ','',$datas['goods_type_colour']);
		$data['shop_price'] = str_replace(' ','',$datas['shop_price']);
		$data['add_time'] = time();

		/*处理数据*/
		$count = M("admin_goods")->where(array("brand_id"=>$brand_id,"goods_name"=>$data['goods_name']))->count();
		
		
		if($count > 0){
		/*判断内存*/		
		   $count1 = M("admin_goods")->where(array("brand_id"=>$brand_id,"goods_name"=>$data['goods_name'],"goods_type_memory"=>$data['goods_type_memory']))->count();
		   
		   if($count1 > 0){
			/*判断颜色*/  
			   $count2 = M("admin_goods")->where(array("brand_id"=>$brand_id,"goods_name"=>$data['goods_name'],"goods_type_memory"=>$data['goods_type_memory'],"goods_type_colour"=>$data['goods_type_colour']))->count();
			   
			    if($count2 > 0){
				   
				   $count3 = M("admin_goods")->where(array("brand_id"=>$brand_id,"goods_name"=>$data['goods_name'],"goods_type_memory"=>$data['goods_type_memory'],"goods_type_colour"=>$data['goods_type_colour'],"shop_price"=>$data['shop_price']))->count();
				   
				    if($count3 == 0){
						
				       /*更新价格*/
						$where['brand_id'] = $brand_id;
						$where['goods_name'] = $data['goods_name'];
						$where['goods_type_memory'] = $data['goods_type_memory'];
						$where['goods_type_colour'] = $data['goods_type_colour'];
				       
					    $data2['shop_price'] = $data['shop_price'];
					    $data2['add_time'] = time();
					   
					    $ret = M("admin_goods")->where($where)->save($data2);
					
					
				    }
				   
			    }else{
				   
				   $ret = M("admin_goods")->add($data);
				   
			    }
			    
			   
		   }else{
			/*新增*/
			$ret = M("admin_goods")->add($data);
			   
		   }
		   

		}else{
		/*不存在 添加*/	
		
		   $ret = M("admin_goods")->add($data);
			
		}

		return $ret;
	}

	
	
	public function shanchu(){

		return $result=$this->where('id='.$_GET['id'])->delete();
	
	}
	
	
	
	
}